﻿using System;
using System.Data;
using Microsoft.ApplicationBlocks.Data;
using viForum.Providers;
using visinia.SmartEngine.Providers;

namespace viForum
{
    public class SqlDataProvider : viForumDataProvider
    {
        #region INIT
        private static string _sqlConnectionString = null;

        public SqlDataProvider()
        {
            _sqlConnectionString = ProviderController.GetDataProviderConnectionString();
        }
        public string ConnectionString
        {
            get { return _sqlConnectionString; }
        }
        #endregion

        #region FORUM
        public override void AddForum(int CategoryId, string Title, string Description)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viForumAddForum", CategoryId, Title, Description);
        }

        public override void UpdateForum(int ForumId, int CategoryId, string Title, string Description)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viForumUpdateForum", ForumId, CategoryId, Title, Description);
        }

        public override void DeleteForum(int ForumId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viForumDeleteForum", ForumId);
        }

        public override IDataReader GetForums()
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viForumGetForums");
        }
        public override IDataReader GetPostCount(int ForumId)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viForumGetPostCount", ForumId);
        }
        #endregion

        #region THREAD
        public override IDataReader AddThread(int ForumId, string UserName, string Title, DateTime Date, string Content)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viForumAddThread", ForumId, UserName, Title, Date, Content);
        }

        public override void UpdateThread(int ThreadId, int ForumId, string UserName, string Title, DateTime Date, string Content)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viForumUpdateThread", ThreadId, ForumId, UserName, Title, Date, Content);
        }

        public override void DeleteThread(int ThreadId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viForumDeleteThread", ThreadId);
        }

        public override IDataReader GetThreads(int ForumId)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viForumGetThreads", ForumId);
        }
        public override IDataReader GetThread(int ThreadId)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viForumGetThread", ThreadId);
        }
        public override IDataReader GetThreadCount(int ForumId)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viForumGetThreadCount", ForumId);
        }
        public override IDataReader GetLatestThread(int ForumId)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viForumGetLatestThread", ForumId);
        }
        public override IDataReader GetLatestThreads(int Count)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viForumGetLatestThreads", Count);
        }
        #endregion

        #region REPLY
        public override IDataReader AddReply(int ThreadId, string UserName, DateTime Date, string Content)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viForumAddReply", ThreadId, UserName, Date, Content);
        }

        public override void UpdateReply(int ReplyId, int ThreadId, string UserName, DateTime Date, string Content)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viForumUpdateReply", ReplyId, ThreadId, UserName, Date, Content);
        }

        public override void DeleteReply(int ReplyId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viForumDeleteReply", ReplyId);
        }

        public override IDataReader GetReplies(int ThreadId)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viForumGetReplies", ThreadId);
        }
        public override IDataReader GetReply(int ReplyId)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viForumGetReply", ReplyId);
        }
        public override IDataReader GetRepliesCount(int ThreadId)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viForumGetRepliesCount", ThreadId);
        }
        public override IDataReader GetLatestReply(int ThreadId)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viForumGetLatestReply", ThreadId);
        }
        #endregion

        #region FORUM SETTINGS
        public override System.Data.IDataReader GetForumSettings()
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viForumGetForumSettings");
        }

        public override void AddForumSetting(string key, string value)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viForumAddForumSetting", key, value);
        }

        public override void UpdateForumSetting(string key, string value)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viForumUpdateForumSetting", key, value);
        }

        public override void DeleteForumSetting(string key)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viForumDeleteForumSetting", key);
        }
        #endregion

        #region CATEGORY
        public override void AddCategory(string Title)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viForumAddCategory", Title);
        }

        public override void UpdateCategory(int CategoryId, string Title)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viForumUpdateCategory", CategoryId, Title);
        }

        public override void DeleteCategory(int CategoryId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viForumDeleteCategory", CategoryId);
        }

        public override IDataReader GetCategories()
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viForumGetCategories");
        }
        #endregion

        #region USER
        public override void AddUser(string UserName, int PostsCount)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viForumAddUser", UserName, PostsCount);
        }

        public override void DeleteUser(string UserName)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viForumDeleteUser", UserName);
        }

        public override void UpdateUser(string UserName, int PostsCount)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viForumUpdateUser", UserName, PostsCount);
        }

        public override IDataReader GetUser(string UserName)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viForumGetUser", UserName);
        }
        #endregion
    }
}
